package com.kb.controller.digikey;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kb.common.ApiResponse;

import com.kb.entity.digikey.ProductData;
import com.kb.entity.digikey.QueryTO;
import com.kb.entity.digikey.RequestInfo;
import com.kb.service.DigikeyService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Controller
@RequestMapping("/digikey")
public class DigikeyController {

    @Resource
    private DigikeyService service;



    @GetMapping("/updateDigikeyToken")
//    @Scheduled(cron = "0 0/9 * * * ?")
    @ResponseBody
    public ApiResponse updateToken()  {
        service.updateToken();
        return ApiResponse.success("ok");
    }

    @PostMapping("/getQuote")
    @ResponseBody
    public CompletableFuture<ApiResponse> getQuote(@RequestBody QueryTO to) throws JsonProcessingException, ExecutionException, InterruptedException {
        // 调用异步方法
        return service.getQuoteAsync(to)
                .thenApply(quote -> ApiResponse.successCount(quote, quote.size()));
    }

    @PostMapping("/excute")
    @ResponseBody
    public ApiResponse excute(@RequestBody RequestInfo request) throws UnsupportedEncodingException {

        switch (request.getType()){
            case 1:
                 return service.excuteMouser();

            case 2:
                return service.excuteDigikey();

            case 3:
                return service.excuteElement();

            case 4:
                return  service.excuteYunhan();

            case 5:
                return service.excuteSHT();




        }



        return ApiResponse.error("失败");
    }



}
